﻿using System.Diagnostics;

namespace Codemonk.Common.Configuring
{
    /// <summary>
    /// 类配置文件，这种配置文件主要是为某一个类单独所用
    /// </summary>
    public class TypeConfigReader : ConfigReader, IConfigReader, IConfigurable
    {
        public TypeConfigReader()
        {
            Reader = new AssemblyConfigReader();
            var frame = new StackFrame(1);
            typeName = frame.GetMethod().DeclaringType.FullName.ToLower();
            Initialization();
        }
        private string typeName;
        private string pathFormat;
        public IConfigReader Reader { get; set; }

        public void Initialization()
        {
            if (Reader == null) return;
            try
            {
                pathFormat = Reader.ReadFirst("/codemonk/configpatterns/configpattern[@name='type']/@pattern");
                ConfigFile = string.Format(pathFormat, typeName);
            }
            catch { throw AssemblyConfigException.Instance; }
        }
    }
}
